package com.ce.sys.role.logic.search;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.ce.framework.datamaintain.logic.ISQLMapper;
import com.ce.framework.datamaintain.logic.SQLSearchCondition;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.StringUtil;
import com.ce.sys.role.view.SysRoleUserMenuView;

/**
 * 嫦娥软件有限公司 版权所有 2013
 * @author andy
 * @since Nov 1, 2011 10:37:34 AM
 * 功能描述：菜单，角色及用户的关联管理功能查询
 * ==================================
 * 修改历史
 * 修改人        修改时间      修改位置（函数名）
 *
 * ==================================
 */
public class SysRoleUserMenuCondition extends SQLSearchCondition
implements ISQLMapper{
    
    /**
     * 帐号名称
     */
    private String userName; 
    
    /**
     * 角色名称
     */
    private String roleName;
    
    /**
     *菜单ID 
     */
    private String menuId;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    
    

    public String getMenuId() {
        return menuId;
    }

    public void setMenuId(String menuId) {
        this.menuId = menuId;
    }

    public Object fromSQLQuery(ResultSet rs, int num) {      
        // TODO Auto-generated method stub
        SysRoleUserMenuView view = new SysRoleUserMenuView();
        try {

            view.setVcId(rs.getString("VC_ID"));    
            view.setRoleId(rs.getString("vc_role$id"));
            view.setRoleName(rs.getString("rolename"));
            view.setMenuId(rs.getString("vc_menu$id"));
            view.setUserId(rs.getString("vc_user$id"));
            view.setUserName(rs.getString("username"));
            view.setVcOperation(rs.getString("VC_OPERATION"));

        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        return view;
    }

    public String getSearchSql() throws AppException {
        // TODO Auto-generated method stub
        StringBuffer sql = new StringBuffer("");
        sql.append("select t1.vc_role$id,t3.vc_name as rolename, t1.vc_menu$id, t2.vc_user$id,t4.vc_name as username,t2.vc_id,t1.VC_OPERATION"
                     +" from sys_t_role$permiss t1,"
                     +"       sys_t_account$role t2,"
                     +"      sys_t_role         t3,"
                     +"      sys_t_person       t4"
                     +" where t2.vc_role$id = t1.vc_role$id(+)"
                     +"  and t2.vc_role$id = t3.vc_id(+)"
                     +"  and t2.vc_user$id = t4.vc_id(+)");
        if(StringUtil.isNotBlank(this.roleName)){
            sql.append(" and t3.vc_name like '%"+this.getSafeValue(this.roleName)+"%'");
        }
        if(StringUtil.isNotBlank(this.userName)){
            sql.append(" and t4.vc_name like '%"+this.getSafeValue(this.userName)+"%'");
        }
        if(StringUtil.isNotBlank(this.menuId)){
            sql.append(" and t1.vc_menu$id = '"+this.getSafeValue(this.menuId)+"'");
        }
        sql.append(" order by t3.vc_seq");
        return sql.toString();
    }

}
